import sqlite3
import datetime
import re


class MyTime(object):
    def __init__(self, h, m,s):
        self.h, self.m ,self.s= int(h), int(m),int(s)

    def __repr__(self):
        return "(%d:%d:%d)" % (self.h, self.m,self.s)
    
    
def adapt_time(tm):
    return "(%d:%d:%d)" %(tm.h, tm.m,tm.s)

def convert_time(s):
    print 'convert time',s
    pattern = re.compile('\((\d{1,})\:(\d{1,})\:(\d{1,})\)')
    result = pattern.findall(s)
    print result
    (h,m,s) = result[0]
    #h,m,s = map(int, s.split(":"))
    return MyTime(h,m,s)


# Register the adapter
sqlite3.register_adapter(MyTime, adapt_time)

# Register the converter
sqlite3.register_converter("mytime", convert_time)

con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = con.cursor()
cur.execute("create table test(d date, ts mytime)")

today = datetime.date.today()

now = MyTime(9,3,30)

cur.execute("insert into test values (?,?)", (today,now))
cur.execute("select d,ts from test")
row = cur.fetchone()
print today, "=>", row[0], type(row[0])

print now, "=>", row[1], type(row[1])
